import 'package:flutter/material.dart';
import 'package:library_base/base/base_app_state_page.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/login_bean_entity.dart';
import 'package:library_base/net/apis.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/route/routers.dart';
import 'package:library_base/utils/sp_util.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/common_scroll_view.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:library_base/widget/textfield/account_text_field.dart';
import 'package:library_base/widget/textfield/pwd_text_field.dart';
import 'package:library_base/utils/other_util.dart';
import 'package:get/get.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:module_common/page/login/login_controller.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends BaseState<LoginPage, LoginController> {
  final TextEditingController _phoneController = TextEditingController();

  final TextEditingController _pwdController = TextEditingController();

  final FocusNode _phoneNode = FocusNode();

  final FocusNode _pwdNode = FocusNode();

  @override
  void onCreate() {
    super.onCreate();
    setStatusBarVisible(false);
    setNavigationBarVisible(false);
  }

  @override
  Widget build(BuildContext context) {
    return super.build(context);
  }

  @override
  List<Widget> getMainChildrenWidget(BuildContext context) {
    return [
      CommonScrollView(
        keyboardConfig:
            OtherUtil.getKeyboardActionsConfig(context, <FocusNode>[_phoneNode, _pwdNode]),
        padding: const EdgeInsets.only(left: 24.0, right: 24.0, top: 20.0),
        children: <Widget>[
          Gaps.vGap60,
          Gaps.vGap32,
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              LocalImage('logo', package: Constant.packCommon(), width: 60, height: 60),
              Expanded(
                flex: 1,
                child: Container(
                    height: 22,
                    padding: const EdgeInsets.only(left: 15),
                    alignment: Alignment.centerLeft,
                    child: Text(MultiLanguage.passwordLogin.tr,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyles.textGray800_w600_20)),
              ),
            ],
          ),
          Gaps.vGap32,
          AccountTextField(
            focusNode: _phoneNode,
            controller: _phoneController,
            onTextChanged: _checkInput,
            areaCode: MultiLanguage.account.tr,
            onPrefixPressed: () {},
          ),
          Gaps.vGap16,
          PwdTextField(
            focusNode: _pwdNode,
            controller: _pwdController,
            onTextChanged: _checkInput,
          ),
          gapH100,
          GetBuilder<LoginController>(builder: (logic) {
            return GradientButton(
              ThemeUtil.getTheme(),
              width: double.infinity,
              height: 77.w,
              text: MultiLanguage.login.tr,
              onPressed: logic.loginEnabled.value ? _login : null,
            );
          }),
        ],
      )
    ];
  }

  @override
  LoginController registerController() => Get.find<LoginController>();

  ///登录
  void _login() async {
    baseGetxController!.requestUniversal<LoginBeanEntity>(Apis.LOGIN,
        params: {"username": _phoneController.text, "password": _pwdController.text},
        resultSuccessCall: (data) {
      SPUtil.token = data.accessToken;
      SPUtil.expiresTime = data.expiresTime;
      SPUtil.refreshToken = data.refreshToken;
      SPUtil.userId = data.userId;
      SPUtil.nickname = data.nickname;
      Get.offAllNamed(Routers.main);
    });
  }

  void _checkInput() {
    if (_phoneController.text.isNotEmpty && _pwdController.text.isNotEmpty) {
      baseGetxController!.loginEnabled.value = true;
    } else {
      baseGetxController!.loginEnabled.value = false;
    }
    baseGetxController!.update();
  }
}
